import 'package:flutter/material.dart';
import 'package:mi_mang_flutter/provider/user_provider.dart';
import 'package:mi_mang_flutter/res/styles.dart';
import 'package:mi_mang_flutter/utils/image_utils.dart';
import 'package:provider/provider.dart';

/// 跳转登陆组件
class WidgetNotLogin extends StatelessWidget {
  const WidgetNotLogin({Key? key, required this.onPressed, this.init})
      : super(key: key);

  /// 登录点击事件
  final VoidCallback onPressed;

  /// 登录后页面刷新初始化方法
  final void Function()? init;

  @override
  Widget build(BuildContext context) {
    return Consumer<UserProvider>(
      builder: (context, provider, _) {
        // 刷新登录状态, 如果变更为已登录状态, 则执行init方法请求数据刷新页面
        if (provider.user != null) {
          if (init != null) init!();
          return Gaps.empty;
        }
        return Scaffold(
          //创建透明层
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.transparent,
          body: Container(
            height: double.infinity,
            width: double.infinity,
            padding: EdgeInsets.symmetric(vertical: 15.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                  child: Gaps.empty,
                  flex: 2,
                ),
                SizedBox(
                  width: 189.0,
                  height: 170.0,
                  child: loadImage('public/not_login'),
                ),
                Gaps.vGap10,
                Text(
                  '您当前未登录账户，无法进行内容操作',
                  style: TextStyle(fontSize: 12.0, color: Colors.grey[600]),
                ),
                Text(
                  '请登录后方可进行操作',
                  style: TextStyle(fontSize: 12.0, color: Colors.grey[600]),
                ),
                Gaps.vGap8,
                MaterialButton(
                  height: 30.0,
                  child: Text(
                    '去登录',
                    style: TextStyle(color: Colors.white, fontSize: 12.0),
                  ),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(15),
                  ),
                  onPressed: onPressed,
                ),
                Expanded(
                  child: Gaps.empty,
                  flex: 3,
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
